#include "widget.h"
#include "ui_widget.h"
#include "iconhelper.h"
#include <QKeyEvent>
#include <QToolButton>
#include <QSound>

#include "control.h"
#include "myhelper.h"
#include "myapp.h"

#include "entertainmentwidget.h"    //添加设备界面
#include "systemsettingdialog.h"    //设置界面
#include "parlourwidget.h"          //客厅
#include "bedroomwidget.h"          //家居界面
#include "statisticwidget.h"        //统计界面
#include "securitywidget.h"         //安防监控界面




Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->initDataTime();
    this->initForm();
    this->initWidget();
    this->initConnect();

}

Widget::~Widget()
{
    delete ui;

    deletWidget();
}

void Widget::initDataTime()
{
    m_timer = new QTimer(this);
    slotShowCurrentDataTime();
    connect(m_timer,SIGNAL(timeout()),
            this,SLOT(slotShowCurrentDataTime()));
    m_timer->start(1000);
}

//界面样式初始化
void Widget::initForm()
{
    this->setWindowTitle(tr("智能网关"));
    setStyleSheet(QLatin1String("QToolButton{border:0px;}"));

    //顶部图标的样式
    s_styleOn = "QLabel{border-image:url(:/images/signal1.png);border:0px;}";
    s_styleOff = "QLabel{border-image:url(:/images/signal0.png);border:0px;}";
    w_styleOn = "QLabel{border-image:url(:/images/wifi1.png);border:0px;}";
    w_styleOff = "QLabel{border-image:url(:/images/wifi0.png);border:0px;}";
    ui->wifi->setStyleSheet(w_styleOff);
    ui->signal->setStyleSheet(s_styleOff);



    //底部菜单的样式

    setToolButtonStyle(ui->tbnSetting,"设置",E_NORMAL,
                       ":/images/bottom/setting.png");
    setToolButtonStyle(ui->tbnHome,"首页",E_NORMAL,
                       ":/images/bottom/home.png");
    setToolButtonStyle(ui->tbnControl,"卧室",E_NORMAL,
                       ":/images/bottom/curtain.png");
    setToolButtonStyle(ui->tbnCurtain,"客厅",E_NORMAL,
                       ":/images/bottom/control.png");
    setToolButtonStyle(ui->tbnMusic,"音乐",E_NORMAL,
                       ":/images/bottom/music.png");
    setToolButtonStyle(ui->tbnModel,"场景",E_NORMAL,
                       ":/images/bottom/reset.png");
    setToolButtonStyle(ui->tbnNight,"监控",E_NORMAL,
                       ":/images/bottom/monit.png");
    setToolButtonStyle(ui->tbnStatistic,"统计",E_NORMAL,
                       ":/images/bottom/statistics.png");
    setToolButtonStyle(ui->tbnRelaxation,"添加",E_NORMAL,
                       ":/images/bottom/add.png");
    ui->stackedWidget->setCurrentIndex(E_HOME_WIDGET);

    m_isPress = false;
    location = this->location;

    ui->tbnModel->setVisible(false);//功能未实现,设置隐藏
    ui->tbnMusic->setVisible(false);//功能未实现,设置隐藏
}

//界面初始化
void Widget::initWidget()
{
    m_settingDialog = new SystemSettingDialog;        //系统设置
    m_bedRoomWidget = new BedRoomWidget;              //卧室
    m_statisticWidget = new StatisticWidget;          //统计
    m_entertainmentWidget = new EntertainmentWidget;  //娱乐->设备搜索
    m_securityWidget = new SecurityWidget;            //安防监控界面
    m_parlourWidget = new Parlourwidget;              //客厅
    m_mymqttWidget= new MyMqtt;                       //通信

    ui->stackedWidget->addWidget(m_parlourWidget);
    ui->stackedWidget->addWidget(m_entertainmentWidget);
    ui->stackedWidget->addWidget(m_bedRoomWidget);
    ui->stackedWidget->addWidget(m_statisticWidget);
    ui->stackedWidget->addWidget(m_securityWidget);



}

void Widget::initConnect()
{

    /*******************卧室客厅互通信号槽*********************/

    //发指令:卧室发给客厅处理
    connect(m_bedRoomWidget,SIGNAL(signalLightLeft(int)),
            m_parlourWidget,SLOT(slotControl(int)));
    connect(m_bedRoomWidget,SIGNAL(signalLightRight(int)),
            m_parlourWidget,SLOT(slotControl(int)));
    //收消息:客厅处理完发给卧室显示
    connect(m_parlourWidget,SIGNAL(signaltobed(int,QString)),
            m_bedRoomWidget,SLOT(slotUpdate(int,QString)));




    /*********************系统设置信号槽**********************/

    connect(m_settingDialog,SIGNAL(settingsignal(QString,QString)),
             m_mymqttWidget,SLOT(mqttInit(QString,QString)));
    connect(m_settingDialog,SIGNAL(signaltomqtt(QString)),
            m_mymqttWidget,SLOT(publish(QString)));
    connect(m_settingDialog,SIGNAL(cancelsignal()),
             m_mymqttWidget,SLOT(cancelconnect()));


//    connect(m_settingDialog,SIGNAL(signalSendSetTempValue(int)),
//            m_parlourWidget,SLOT(slotChangeTempSetValue(int)));
//    connect(m_settingDialog,SIGNAL(signalSendSetHumValue(int)),
//            m_parlourWidget,SLOT(slotChangeHumSetValue(int)));


    /*********************客厅设置信号槽**********************/

       //客厅显示信息的信号槽
       connect(m_mymqttWidget,SIGNAL(upsignal(QString)),
               m_parlourWidget,SLOT(slotUpdate(QString)));
       //客厅解析完json发送给MQTT进行发布的信号槽
       connect(m_parlourWidget,SIGNAL(signaltomqtt(QString)),
               m_mymqttWidget,SLOT(publish(QString)));
    /*********************连接状态信号槽**********************/
       connect(m_mymqttWidget,&MyMqtt::mqttstatesignalon,[=]()
       {
         ui->signal->setStyleSheet(s_styleOn);
         QSound::play(":/sounds/connect.wav");
       });

       connect(m_mymqttWidget,&MyMqtt::mqttstatesignaloff,[=]()
       {
         ui->signal->setStyleSheet(s_styleOff);
         QSound::play(":/sounds/disconnect.wav");
       });

       connect(m_entertainmentWidget,&EntertainmentWidget::tcpconnectsignalon,[=]()
       {
         ui->wifi->setStyleSheet(w_styleOn);
         QSound::play(":/sounds/connect.wav");
       });

       connect(m_entertainmentWidget,&EntertainmentWidget::tcpconnectsignaloff,[=]()
       {
         ui->wifi->setStyleSheet(w_styleOff);
         QSound::play(":/sounds/disconnect.wav");
       });

}



void Widget::deletWidget()
{
    if (m_settingDialog != NULL){
        delete m_settingDialog;
        m_settingDialog = NULL;
    }
    if (m_parlourWidget != NULL){
        delete m_parlourWidget;
        m_parlourWidget = NULL;
    }
    if (m_entertainmentWidget != NULL){
        delete m_entertainmentWidget;
        m_entertainmentWidget = NULL;
    }
    if (m_bedRoomWidget != NULL){
        delete m_bedRoomWidget;
        m_bedRoomWidget = NULL;
    }
    if (m_statisticWidget != NULL){
        delete m_statisticWidget;
        m_statisticWidget = NULL;
    }
    if (m_securityWidget != NULL){
        delete m_securityWidget;
        m_securityWidget = NULL;
    }
}


void Widget::setToolButtonStyle(QToolButton *tbn, const QString &text,
                                int textSize,const QString iconName)
{
    //设置显示的文本
    tbn->setText(text);
    if (textSize == E_BIG)
    {
        tbn->setFont(QFont("微软雅黑",14,QFont::Bold));
    }else if (textSize == E_NORMAL)

    //设置显示的字体
    tbn->setFont(QFont("微软雅黑",10,QFont::Bold));

    tbn->setAutoRaise(true);
    //设置按钮图标
    tbn->setIcon(QPixmap(QString("%1").arg(iconName)));
    tbn->setIconSize(QPixmap(QString("%1").arg(iconName)).size());
    //设置文本在图标下边
    tbn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
}


void Widget::setCurrentWidget(enum_widget enum_widget_name)
{

    ui->stackedWidget->setCurrentIndex(enum_widget_name);
}

//显示当前的日期和时间
void Widget::slotShowCurrentDataTime()
{
    ui->label_hour->setText(QTime::currentTime().toString("hh"));
    ui->label_min->setText(QTime::currentTime().toString("mm"));
    ui->label_sec->setText(QTime::currentTime().toString("ss"));
    ui->label_data->setText(QDate::currentDate().toString("yyyy年MM月dd日"));
    ui->label_week->setText(QDate::currentDate().toString("dddd"));
}

void Widget::slotErrorString(QString err)
{
#if QDEBUG
    qDebug()<<"error:"<<err;
#endif
}

void Widget::keyPressEvent(QKeyEvent *key)
{
    switch (key->key())
    {
        case Qt::Key_Escape:
            this->close();
            break;
        case Qt::Key_Space:
            this->showMinimized();
        default:
            break;
    }
}

void Widget::mouseMoveEvent(QMouseEvent *e)
{
#if _ARM_ == 0
    if (m_isPress && (e->buttons()) &&(Qt::LeftButton))
    {
        this->move(e->globalPos() - mousePoint);
        e->accept();
    }
#endif
}

void Widget::mousePressEvent(QMouseEvent *e)
{
#if _ARM_ == 0
    if (e->button() == Qt::LeftButton)
    {
        m_isPress = true;
        mousePoint = e->globalPos() - this->pos();
        e->accept();
    }
#endif
}

void Widget::mouseReleaseEvent(QMouseEvent *)
{
#if _ARM_ == 0
    m_isPress = false;
#endif
}


//主页面
void Widget::on_tbnHome_clicked()
{
    ui->stackedWidget->setCurrentIndex(E_HOME_WIDGET);
}



//统计界面
void Widget::on_tbnStatistic_clicked()
{
    if (ui->stackedWidget->currentIndex() != E_TEMPTURE)
    {
        this->setCurrentWidget(E_TEMPTURE);

    }
}

//安防监控
void Widget::on_tbnNight_clicked()
{
    if (ui->stackedWidget->currentIndex() != E_SECURITY_WIDGET)
    {
        this->setCurrentWidget(E_SECURITY_WIDGET);

    }
}


void Widget::slot_receiveData(QByteArray buff)
{
    qDebug()<<myHelper::ByteArrayToHexStr(buff);
}

//卧室界面
void Widget::on_tbnControl_clicked()
{
    if (ui->stackedWidget->currentIndex() != E_BEDROOM_WIDGET)
    {
        this->setCurrentWidget(E_BEDROOM_WIDGET);

    }
}


//系统设置界面
void Widget::on_tbnSetting_clicked()
{
    m_settingDialog->exec();

}
//设备搜索模块
void Widget::on_tbnRelaxation_clicked()
{
    if (ui->stackedWidget->currentIndex() != E_ENTERTAINMENT_WIDGET)
    {
        this->setCurrentWidget(E_ENTERTAINMENT_WIDGET);

    }
}
//客厅界面
void Widget::on_tbnCurtain_clicked()
{
    if (ui->stackedWidget->currentIndex() != E_PARLOUR_WIDGET)
    {
        this->setCurrentWidget(E_PARLOUR_WIDGET);

    }
}
